/*
 * Created by JFormDesigner on Sun Jun 28 11:50:05 CST 2020
 */

package mynote;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.border.*;

/**
 * @author Shawn Lou
 */
//注册对话框
public class SignUpDialog extends JDialog {
    private static final String userTip = "1~8位用户名";
    private static final String pwdTip = "8~12位字母和数字组成的密码";

    public SignUpDialog(Window owner) {
        super(owner);
        initComponents();
        setIconImage(new ImageIcon("src/mynote/data/icon.png").getImage());
        btnSignUp.requestFocus();
        btnSignUp.addActionListener(new SignUpBtnListener(this));
        txtUsername.addFocusListener(new TextFocusListener(userTip, txtUsername));
        pwdPassword.addFocusListener(new PwdCheckListener(pwdTip, pwdPassword, this));
        pwdPasswordCon.addFocusListener(new PwdFocusListener("确认密码", pwdPasswordCon));
        btnCancel.addActionListener(e -> dispose());

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        txtUsername = new JTextField();
        pwdPassword = new JPasswordField();
        pwdPasswordCon = new JPasswordField();
        buttonBar = new JPanel();
        btnSignUp = new JButton();
        btnCancel = new JButton();

        //======== this ========
        setTitle("Sign Up");
        setModal(true);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        var contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {

                //---- txtUsername ----
                txtUsername.setToolTipText("\u7528\u6237\u540d");
                txtUsername.setBorder(new EmptyBorder(4, 4, 4, 4));

                //---- pwdPassword ----
                pwdPassword.setToolTipText("\u5bc6\u7801");
                pwdPassword.setBorder(new EmptyBorder(4, 4, 4, 4));
                pwdPassword.setMargin(new Insets(2, 6, 2, 6));

                //---- pwdPasswordCon ----
                pwdPasswordCon.setToolTipText("\u786e\u8ba4\u5bc6\u7801");
                pwdPasswordCon.setBorder(new EmptyBorder(4, 4, 4, 4));
                pwdPasswordCon.setMargin(new Insets(2, 6, 2, 6));

                GroupLayout contentPanelLayout = new GroupLayout(contentPanel);
                contentPanel.setLayout(contentPanelLayout);
                contentPanelLayout.setHorizontalGroup(
                    contentPanelLayout.createParallelGroup()
                        .addGroup(contentPanelLayout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(contentPanelLayout.createParallelGroup()
                                .addComponent(txtUsername)
                                .addGroup(GroupLayout.Alignment.TRAILING, contentPanelLayout.createSequentialGroup()
                                    .addGap(0, 0, Short.MAX_VALUE)
                                    .addComponent(pwdPasswordCon, GroupLayout.PREFERRED_SIZE, 247, GroupLayout.PREFERRED_SIZE))
                                .addComponent(pwdPassword))
                            .addContainerGap())
                );
                contentPanelLayout.setVerticalGroup(
                    contentPanelLayout.createParallelGroup()
                        .addGroup(contentPanelLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(txtUsername, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(pwdPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(pwdPasswordCon, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(10, Short.MAX_VALUE))
                );
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));

                //---- btnSignUp ----
                btnSignUp.setText("\u6ce8\u518c");
                btnSignUp.setBackground(SystemColor.activeCaption);
                btnSignUp.setFocusPainted(false);

                //---- btnCancel ----
                btnCancel.setText("\u53d6\u6d88");
                btnCancel.setBackground(SystemColor.activeCaption);
                btnCancel.setFocusPainted(false);

                GroupLayout buttonBarLayout = new GroupLayout(buttonBar);
                buttonBar.setLayout(buttonBarLayout);
                buttonBarLayout.setHorizontalGroup(
                    buttonBarLayout.createParallelGroup()
                        .addGroup(buttonBarLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(btnSignUp, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                            .addComponent(btnCancel, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())
                );
                buttonBarLayout.setVerticalGroup(
                    buttonBarLayout.createParallelGroup()
                        .addGroup(buttonBarLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(btnCancel)
                            .addComponent(btnSignUp))
                );
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    //检查密码是否合法
    public boolean CheckPwd() {
        String pwd = String.valueOf(pwdPassword.getPassword());
        //8~12位字母和数字组成的密码
        if (pwd.matches("^[a-zA-Z0-9]{8,12}$"))
            return true;
        JOptionPane.showConfirmDialog(null,"密码不符合要求！",
                "", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
        return false;
    }
    //检查用户名是否合法
    public boolean CheckName() {
        String username = txtUsername.getText();
        //1~8位用户名
        if (username.length() >= 1 && username.length() <= 8 && !username.equals(userTip))
            return true;
        JOptionPane.showConfirmDialog(null,"用户名不符合要求！",
                "", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
        return false;
    }

    //检查确认密码是否与密码一致
    public boolean CheckPwdConfirm() {
        String pwd = String.valueOf(pwdPassword.getPassword());
        String pwdCon = String.valueOf(pwdPasswordCon.getPassword());
        if (pwd.equals(pwdCon))
            return true;
        JOptionPane.showConfirmDialog(null,"确认密码不一致！",
                "", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
        return false;
    }

    //注册
    public void signUp() {
        String username = txtUsername.getText();
        String password = String.valueOf(pwdPassword.getPassword());
        Connection con = null;
        Statement sta = null;
        ResultSet res = null;
        try {
            Class.forName(MyDB.driver);
            con = DriverManager.getConnection(MyDB.url, MyDB.userDB, MyDB.pwdDB);
            sta = con.createStatement();
            res = sta.executeQuery("select username, password from user");
            while (res.next()) {
                String name = res.getString(1);
                if (username.equals(name)) {
                    //用户已存在
                    JOptionPane.showConfirmDialog(null,"用户已存在！",
                            "注册失败", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
                    return;
                }
            }
            //在数据库插入条目
            sta.executeUpdate("insert into user values('"+username+"','"+password+"')");
            JOptionPane.showConfirmDialog(null,"注册成功！",
                    "", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            MyDB.closeDB(con, sta, res);
        }
    }
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JTextField txtUsername;
    private JPasswordField pwdPassword;
    private JPasswordField pwdPasswordCon;
    private JPanel buttonBar;
    private JButton btnSignUp;
    private JButton btnCancel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
